<%@page import="java.io.InputStream"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.List"%>
<%@page import="java.io.File"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	String fpath = "d:/uploadImage/";
	DiskFileItemFactory factory = new DiskFileItemFactory();
	factory.setSizeThreshold(1024*1024*5);	// 5MB
	factory.setRepository(new File(fpath)); // 업로드 경로
	
	ServletFileUpload upload = new ServletFileUpload(factory);
	upload.setFileSizeMax(1*1024*1024);
	
	List<?> items = upload.parseRequest(request);
	Iterator iter = items.iterator();
	
	while (iter.hasNext()) {
		FileItem item = (FileItem)iter.next();
		if (item.isFormField()) {
			out.print(item.getString("UTF-8")+"<br />");
			out.print(item.getFieldName()+"<br />");
		} else {
			out.print(item.getName()+"<br />");
			out.print(item.getSize()+"<br />");
			out.print(item.getContentType()+"<br />");
			out.print(item.getFieldName()+"<br />");
			out.print(item.getString()+"<br />");
			
			// 파일명 추출
			int pos = item.getName().lastIndexOf("\\");
			String fname = item.getName().substring(pos+1);			
			// 파일저장
			if (item.getSize()>0) {
				int ext = (int)System.currentTimeMillis()/1000;
				Thread.sleep(1000);
				File save = new File(fpath + ext + fname);
				item.write(save);
			}
			
		}
	}
	
	
	
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Insert title here</title>
</head>
<body>

</body>
</html>